package com.hisan.freeride.home.adapter;

import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hisan.freeride.R;
import com.hisan.freeride.common.event.FirstEvent;
import com.hisan.freeride.home.model.MyAddress;


import org.greenrobot.eventbus.EventBus;

import java.util.List;


/**
 * 地址适配器
 * Created by admin on 2017/5/15.
 */

public class SiteAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private List<MyAddress>data;
    //动态添加的布局文件布局
    View view = null;


    public SiteAdapter(Activity context) {
        this.inflater = LayoutInflater.from(context);

    }

    public void setData(List<MyAddress> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data != null ? data.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.adress_list_item, null);
            viewHolder = new ViewHolder();
            viewHolder.id = (TextView) convertView.findViewById(R.id.id);
            viewHolder.name = (TextView) convertView.findViewById(R.id.name);
            viewHolder.phone = (TextView) convertView.findViewById(R.id.phone);
            viewHolder.provinces = (TextView) convertView.findViewById(R.id.province);
            viewHolder.city= (TextView) convertView.findViewById(R.id.city);
            viewHolder.area= (TextView) convertView.findViewById(R.id.area);
            viewHolder.street = (TextView) convertView.findViewById(R.id.way);
            viewHolder.layout = (LinearLayout) convertView.findViewById(R.id.layout);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.id.setText(data.get(position).getId() + "");
        viewHolder.name.setText(data.get(position).getName());
        viewHolder.phone.setText(data.get(position).getMobile());
        viewHolder.provinces.setText(data.get(position).getProvince());
        viewHolder.city.setText(data.get(position).getCity());
        viewHolder.area.setText(data.get(position).getArea());
        viewHolder.street.setText(data.get(position).getStreet());
        view = inflater.inflate(R.layout.sitelayout_inflate, null);
            ImageView myset_item_isdefault= (ImageView) view.findViewById(R.id.myset_item_isdefault);
            //删除
        LinearLayout myset_delete= (LinearLayout) view.findViewById(R.id.m_delete);
            myset_delete.setOnClickListener(v -> EventBus.getDefault().post(new FirstEvent(data.get(position).getId(),2,position)));
        //编辑
        LinearLayout myset_edit= (LinearLayout) view.findViewById(R.id.myset_edit);
        myset_edit.setOnClickListener(v -> EventBus.getDefault().post(new FirstEvent(data.get(position).getId(),3,position)));
            //设为默认
            LinearLayout myset_item_isliner= (LinearLayout) view.findViewById(R.id.myset_item_isline);
            myset_item_isliner.setOnClickListener(v -> EventBus.getDefault().post(new FirstEvent(data.get(position).getId(),1,position)));
            if (data.get(position).getIs_default()==0) {
                myset_item_isdefault.setImageResource(R.mipmap.radio_null);
            } else {
                myset_item_isdefault.setImageResource(R.mipmap.radiocheck);
            }
            viewHolder.layout.addView(view);
        return convertView;
    }

    class ViewHolder {
        TextView name, provinces,city,area, street, phone, id;
        LinearLayout layout;
    }
}